package com.jiyun.service.impl;

import com.alibaba.nacos.api.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.QuizMapper;
import com.jiyun.model.Quiz;
import com.jiyun.service.QuizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuizServiceImpl extends ServiceImpl<QuizMapper, Quiz> implements QuizService {
    @Autowired
    QuizMapper quizMapper;

    @Override
    public List<Quiz> quizList() {
        List<Quiz> list1 = new ArrayList<>();

        LambdaQueryWrapper<Quiz> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.orderByDesc(Quiz::getId);

        List<Quiz> list = quizMapper.selectList(lambdaQueryWrapper);

        list1.clear();

        for (int i = 0; i < 3; i++) {
            list1.add(list.get(i));
        }

        for (Quiz quiz : list1) {
            Integer q= quizMapper.findQuiz(quiz.getId());
            quiz.setCount(q);
            if (StringUtils.isEmpty(quiz.getCid()) ||quiz.getCid().equals("")){
                quiz.setNum(0);
            }else {
                String[] split = quiz.getCid().split(",");
                quiz.setNum(split.length);
                System.out.println(quiz.getNum());
            }
        }

        return list1;
    }
}
